package com.ny.dao.impl;
import com.ny.dao.ProductDao;
import com.ny.entity.Product;
import com.ny.until.SqlConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ProductDaoImpl implements ProductDao {
    Connection connection = SqlConnection.getCon();

    public ProductDaoImpl() throws SQLException, ClassNotFoundException {
    }
    @Override
    public List<Product> findAllProduct() throws SQLException {
        String sql = "select * from products";
        PreparedStatement  preparedStatement = connection.prepareStatement(sql);
        ResultSet  resultSet = preparedStatement.executeQuery();
        List<Product> productList = new ArrayList<>();
        while(resultSet.next()){
            Product product = new Product();
            product.setProduct_id(resultSet.getInt("product_id"));
            product.setName(resultSet.getString("name"));
            product.setPrice(resultSet.getDouble("price"));
            product.setStock(resultSet.getInt("stock"));
            product.setCategory(resultSet.getString("category"));
            product.setDescription(resultSet.getString("description"));
            productList.add(product);

        }
        return productList;
    }

    @Override
    public Product findProductById(int productId) throws SQLException {
        String sql = "SELECT * FROM products WHERE product_id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, productId);
            ResultSet resultSet= preparedStatement.executeQuery();
            while(resultSet.next()) {
                Product product = new Product();
                product.setProduct_id(resultSet.getInt("product_id"));
                product.setName(resultSet.getString("name"));
                product.setPrice(resultSet.getDouble("price"));
                product.setStock(resultSet.getInt("stock"));
                product.setCategory(resultSet.getString("category"));
                product.setDescription(resultSet.getString("description"));
                return product;
            }
        return null;
    }

    @Override
    public boolean updateProductStock(int productId, int number) throws SQLException {
        String sql = "UPDATE products SET stock = stock - ? WHERE product_id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, number);
        preparedStatement.setInt(2, productId);
        int rowsAffected = preparedStatement.executeUpdate();
        return rowsAffected > 0;
    }

    @Override
    public int updateStudentPassword(int studentId, String studentPassword) throws SQLException {
        String sql = "UPDATE students SET password = ? WHERE student_id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, studentPassword);
        preparedStatement.setInt(2, studentId);
        return preparedStatement.executeUpdate();
    }

    @Override
    public int addProduct(Product product) throws SQLException {
        String sql = "INSERT INTO products (product_id,name, price, stock, category, description) VALUES (?,?, ?, ?, ?, ?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, product.getProduct_id());
        preparedStatement.setString(2, product.getName());
        preparedStatement.setDouble(3, product.getPrice());
        preparedStatement.setInt(4, product.getStock());
        preparedStatement.setString(5, product.getCategory());
        preparedStatement.setString(6, product.getDescription());
        return preparedStatement.executeUpdate();
    }

    @Override
    public boolean deleteProduct(int deleteId) throws SQLException {
        String sql = "DELETE FROM products WHERE product_id = ?";
        PreparedStatement  preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, deleteId);
        return preparedStatement.executeUpdate() > 0;
    }
}
